<?php
/*******************************************************************************
 * $Date:: May 30, 2011 7:28:34 AM                                             $
* $Revision:                                                                  $
* $Author:: Tuan Duong                                                        $
* $Comment::                                                                  $
******************************************************************************/

/**
 * User Adapter
 *
 * @author	Tuan Duong <duongthaso[at]gmail[dot]com>
 * @version
 */

class Ovis_Auth_UserAdapter implements Zend_Auth_Adapter_Interface
{
	protected $username;
	protected $password;

	public function __construct($params = array())
	{
		if (! isset ($params['username']) || ! isset($params['password'])){
			throw new Sss_Exception_Abstract(Resources::_('INVALID_USERNAME_PASSOWRD'));
		}
		$this->username = $params['username'];
		$this->password = $params['password'];
	}

	/**
	 * @see Zend_Auth_Adapter_Interface::authenticate()
	 */
	public function authenticate()
	{
		$userTable = new Application_Model_Mapper_Account();
		$user = $userTable->getUser($this->username);
		if($user != false)
			$user = (array)$user;
		$crypt = Sss_Crypt::factory();
		$encryptedPassword = $crypt->encrypt($this->password);
		if ($user != false && $encryptedPassword == $user['password']) {
			$identity = new stdClass();
			$identity->username = $user['username'];
			$identity->email = $user['email'];
			$identity->accountid = $user['accountid'];
			$identity->fullName = $user['fullname'];
			$identity->accounttype = $user['accounttype'];
			$identity->otrs_user_id = $user['otrs_user_id'];
			return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity, array());
		}
		return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->username);
	}
}